package part15;

import java.util.Date;

/**
 * @author wcj
 * @Date 2021/5/30 18:49
 * @Version 1.0
 */
public class Part1537 {

    public static void main(String[] args) {
        Mixin mix1 = new Mixin();
        Mixin mix2 = new Mixin();
        mix1.set("test string 1");
        mix2.set("test string 2");
        System.out.println(mix1.get() + ":timestamp:" + mix1.getTimeStamp() + ",number:" + mix1.getSerialNumber());
        System.out.println(mix2.get() + ":timestamp:" + mix2.getTimeStamp() + ",number:" + mix2.getSerialNumber());
    }
}

/**
 * 混型：即一个类混合多个类的能力，java中通常使用组合或者继承来做。即接口的方式来实现混型
 */
class Mixin extends BasicImp implements Timestamp, SerialNumber {
    private TimeStampImp timeStampImp = new TimeStampImp();
    private SerialNumberImp serialNumberImp = new SerialNumberImp();

    @Override
    public long getTimeStamp() {
        return timeStampImp.getTimeStamp();
    }

    @Override
    public long getSerialNumber() {
        return serialNumberImp.getSerialNumber();
    }
}

interface Timestamp {
    /**
     * getTimeStamp
     *
     * @return
     */
    long getTimeStamp();
}

class TimeStampImp implements Timestamp {
    private final long timestamp;

    public TimeStampImp() {
        timestamp = System.currentTimeMillis();
    }

    @Override
    public long getTimeStamp() {
        return timestamp;
    }
}

interface SerialNumber {
    /**
     * getSerialNumber
     *
     * @return
     */
    long getSerialNumber();
}

class SerialNumberImp implements SerialNumber {
    private static long counter = 0;
    private static long serialNumber = counter++;

    @Override
    public long getSerialNumber() {
        return serialNumber;
    }
}

interface Basic {
    public void set(String val);

    public String get();
}

class BasicImp implements Basic {
    private String value;

    @Override
    public void set(String val) {
        this.value = val;
    }

    @Override
    public String get() {
        return value;
    }
}


